package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.AppliancesCompositionsModel;
import application.model.modify.ApplianceCompositionModel;
import application.translators.Translator;
import application.translators.impl.AppliancesCompositionTranslator;
import dao.AppliancesCompositionDao;
import mappings.AppliancesComposition;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("appliancesCompositions")
public class AppliancesCompositionsService
{
    public AppliancesCompositionDao appliancesCompositionDao = new AppliancesCompositionDao();
    public Translator<List<AppliancesComposition>, AppliancesCompositionsModel> translator = new AppliancesCompositionTranslator();

    @Path("getForMaterial/{materialId}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public AppliancesCompositionsModel getAllAppliancesCompositionsForMaterial(@PathParam("materialId") int materialId)
    {
        return translator.translate(appliancesCompositionDao.getAppliancesCompositionsForMaterial(materialId));
    }

    @Path("addApplianceComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ApplianceCompositionModel addProducentComposition(@FormParam("materialId") int materialId,
                                                             @FormParam("standardId") int standardId,
                                                             @FormParam("applianceId") int applianceId)
    {
        ApplianceCompositionModel applianceCompositionModel = new ApplianceCompositionModel();

        try
        {
            AppliancesComposition appliancesComposition = appliancesCompositionDao.addProducentComposition(materialId, standardId, applianceId);
            applianceCompositionModel.setId(appliancesComposition.getId());
            applianceCompositionModel.setStatus(0);
            applianceCompositionModel.setMaterialName(appliancesComposition.getMaterial().getName());
            applianceCompositionModel.setApplianceName(appliancesComposition.getAppliances().getName());
            applianceCompositionModel.setStandardName(appliancesComposition.getStandards().getName());
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            applianceCompositionModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            applianceCompositionModel.setStatus(2);
        }

        return applianceCompositionModel;
    }

    @Path("editApplianceComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ApplianceCompositionModel editProducentComposition(@FormParam("id") int id,
                                                              @FormParam("materialId") int materialId,
                                                              @FormParam("standardId") int standardId,
                                                              @FormParam("applianceId") int applianceId)
    {
        ApplianceCompositionModel applianceCompositionModel = new ApplianceCompositionModel();

        try
        {
            AppliancesComposition appliancesComposition = appliancesCompositionDao.editProducentComposition(id, materialId, standardId, applianceId);
            applianceCompositionModel.setId(appliancesComposition.getId());
            applianceCompositionModel.setStatus(0);
            applianceCompositionModel.setStandardName(appliancesComposition.getStandards().getName());
            applianceCompositionModel.setMaterialName(appliancesComposition.getMaterial().getName());
            applianceCompositionModel.setApplianceName(appliancesComposition.getAppliances().getName());
        }
        catch (ItemNotFoundException e)
        {
            applianceCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            applianceCompositionModel.setStatus(2);
        }

        return applianceCompositionModel;
    }

    @Path("deleteApplianceComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ApplianceCompositionModel deleteProducentComposition(@FormParam("id") Integer id)
    {
        ApplianceCompositionModel applianceCompositionModel = new ApplianceCompositionModel();

        try
        {
            appliancesCompositionDao.deleteApplianceComposition(id);
            applianceCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            applianceCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            applianceCompositionModel.setStatus(2);
        }

        return applianceCompositionModel;
    }
}
